package de.listrong.da.metainfo.datamodel;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import de.listrong.da.tools.IdGenerator;

/**
 * Created by IntelliJ IDEA.
 * User: Qiang
 * Date: 10.11.2008
 * Time: 17:03:02
 */
public class MetaInfoList {

    private static LinkedList<MetaInfo> metaList = new LinkedList<MetaInfo>();
    private static HashMap<String, MetaInfo> metaInfoMap = new HashMap<String, MetaInfo>();

    public static HashMap<String, MetaInfo> readFile(File filename) {
        try {
            SAXBuilder sb = new SAXBuilder();
            Document metaData = sb.build(filename);

            // namespace in metainfos.xml
            Namespace msNS = Namespace.getNamespace("b", "http://schemas.microsoft.com/office/word/2004/10/bibliography");
            Namespace defaultNS = Namespace.getNamespace( "http://example.com/docmeta");
            
            Element root = metaData.getRootElement();
            List aList = root.getChildren("metainfo", defaultNS);
            for( Object listItem: aList ) {
                IdGenerator.getInstance().clear();  // reset id for each docuement

                Element metaInfoElement = (Element)listItem;
                String key =  metaInfoElement.getAttribute("resource").getValue();
                Element sourceElement = metaInfoElement.getChild("Source", msNS);

                MetaInfo aMetaInfo = new MetaInfo();

                aMetaInfo.setSourceType( sourceElement.getChild("SourceType", msNS).getText());
                aMetaInfo.setTitleText(sourceElement.getChild("Title", msNS).getText());
                aMetaInfo.setPublishCity(sourceElement.getChild("City", msNS).getText());
                aMetaInfo.setPublisher(sourceElement.getChild("Publisher", msNS).getText());
                aMetaInfo.setPublishYear(sourceElement.getChild("Year", msNS).getText());
                List personList =
                        sourceElement.getChild("Author", msNS).getChild("Author", msNS).getChild("NameList", msNS).getChildren("Person", msNS);
                aMetaInfo.setAuthorList(getAuthorList(personList));
                
                metaInfoMap.put(key, aMetaInfo);
            } // end for

        } catch(Exception e) {
             e.printStackTrace(); 
        } // end try ... catch block
        return metaInfoMap;
    }

    private static List<Author> getAuthorList(List personList) {
        Namespace msNS = Namespace.getNamespace("b", "http://schemas.microsoft.com/office/word/2004/10/bibliography");
        
        List<Author> authorList = new LinkedList<Author>();

        for(int i = 0; i< personList.size(); i++ ){
            Element personElement = (Element)personList.get(i);

            String firstname = personElement.getChild("First", msNS).getText();
            String lastname = personElement.getChild("Last", msNS).getText();
            Author aAuthor = new Author(firstname, lastname);

            authorList.add(aAuthor);
        }
         return authorList;
    }

}
